/**
 * Created on 2008-8-10
 * @author Dennis Huang
 */
package shmged.com.shlcm.fc;

import java.io.Serializable;

import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;

/**
 * Created on 2008-8-10
 * @author Dennis Huang
 */
public final class CmPersistenceManagerFwd implements CmPersistenceManager, RemoteAccess, Serializable {
   private static final long          serialVersionUID = -3984252416340253197L;

   private final CmPersistenceManager instance         = new CmStandardPersistenceManager();

   public CmQueryResult find(CmQuerySpec qs) throws Exception {
      if (!RemoteMethodServer.ServerFlag) {
         String method = "find";
         Class[] types = {CmQuerySpec.class};
         Object[] values = {qs};

         return (CmQueryResult) RemoteMethodServer.getDefault().invoke(method, null, this, types, values);
      }

      return instance.find(qs);
   }
   
   public CmPersistable find(Class klass, Object keyId) throws Exception {
      if (!RemoteMethodServer.ServerFlag) {
         String method = "find";
         Class[] types = {Class.class, Object.class};
         Object[] values = { klass, keyId};

         return (CmPersistable) RemoteMethodServer.getDefault().invoke(method, null, this, types, values);
      }

      return instance.find(klass, keyId);
   }

   public boolean isPersistence(CmPersistable p) throws Exception {
      if (!RemoteMethodServer.ServerFlag) {
         String method = "isPersistence";
         Class[] types = {CmPersistable.class};
         Object[] values = {p};

         return ((Boolean) RemoteMethodServer.getDefault().invoke(method, null, this, types, values)).booleanValue();
      }

      return instance.isPersistence(p);
   }

   public void save(CmPersistable p) throws Exception {
      if (!RemoteMethodServer.ServerFlag) {
         String method = "save";
         Class[] types = {CmPersistable.class};
         Object[] values = {p};

         RemoteMethodServer.getDefault().invoke(method, null, this, types, values);
         return;
      }

      instance.save(p);
   }

   public void insert(CmPersistable p) throws Exception {
      if (!RemoteMethodServer.ServerFlag) {
         String method = "insert";
         Class[] types = {CmPersistable.class};
         Object[] values = {p};

         RemoteMethodServer.getDefault().invoke(method, null, this, types, values);
         return;
      }

      instance.insert(p);
   }

   public void update(CmPersistable p) throws Exception {
      if (!RemoteMethodServer.ServerFlag) {
         String method = "update";
         Class[] types = {CmPersistable.class};
         Object[] values = {p};

         RemoteMethodServer.getDefault().invoke(method, null, this, types, values);
         return;
      }

      instance.update(p);
   }

   public void delete(CmPersistable p) throws Exception {
      if (!RemoteMethodServer.ServerFlag) {
         String method = "delete";
         Class[] types = {CmPersistable.class};
         Object[] values = {p};

         RemoteMethodServer.getDefault().invoke(method, null, this, types, values);
         return;
      }

      instance.delete(p);
   }
   
   public CmQueryResult find(CmQuerySpec qs,String sql) throws Exception {
	      if (!RemoteMethodServer.ServerFlag) {
	         String method = "find";
	         Class[] types = {CmQuerySpec.class,String.class};
	         Object[] values = {qs,sql};

	         return (CmQueryResult) RemoteMethodServer.getDefault().invoke(method, null, this, types, values);
	      }

	      return instance.find(qs,sql);
   }
}
